home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ldb.zip / SBDRDEM1.CPP < prev    next >
C/C++ Source or Header  |  1991-10-18  |  4KB  |  180 lines

  1.     // sbdrdem1.cpp
  2.     // Demo Streamable Binder with streamable nodes
  3.     // Redirect cout to a file for this demo!
  4.     // Link with binder.obj and sbinder.obj
  5.  
  6.     #include <fstream.h>
  7.     #include <iomanip.h>
  8.     #include "sbinder.hpp"
  9.  
  10.  
  11.     #define ID_StreamableInt 4
  12.     #define ID_SINT_CMP  1
  13.     #define ORIG_NODES   4
  14.  
  15.     class StreamableInt : Streamable  {
  16.         int i;
  17.     protected:
  18.         void construct(int i)
  19.             { this->i = i; }
  20.     public:
  21.         STREAMABLE(StreamableInt,
  22.             ID_StreamableInt,
  23.             Streamable);
  24.         StreamableInt(int i)
  25.             : Streamable(UNIQUE_STREAMABLE,
  26.             ID_CLASS)
  27.             { construct(i); }
  28.         int Int() { return i; }
  29.         ~StreamableInt() {}
  30.     };
  31.     typedef StreamableInt * StreamableInT;
  32.     #define StreamableInT0 ((StreamableInT)0)
  33.  
  34.  
  35.     ostream& StreamableInt::store(ostream& os)
  36.         { return os << i << endm; }
  37.  
  38.     StreamablE StreamableInt::load(istream& is,
  39.         StreamablE InstancE)
  40.     {
  41.         int i;
  42.  
  43.         if (!(is >> i >> nextm))  {
  44.             lserror("loading StreamableInt",
  45.                 ID_CLASS);
  46.             return StreamablE0;
  47.         }
  48.         if (!InstancE)
  49.             if ((InstancE =    (StreamablE)
  50.                 new StreamableInt
  51.                 (UNIQUE_STREAMABLE))
  52.                 == StreamablE0)  {
  53.                 lserror("unable to construct"
  54.                     " StreamableInt",
  55.                     ID_CLASS);
  56.                 return StreamablE0;
  57.             }
  58.         ((StreamableInT)InstancE)->construct(i);
  59.         return InstancE;
  60.  
  61.     }
  62.  
  63.     int sintcmp(const StreamableInT I1,
  64.         const StreamableInT I2)
  65.         { return (I1->Int() - I2->Int()); }
  66.  
  67.     void display(StreamableInT I)
  68.     {
  69.         cout << "Address of node: "
  70.             << setw(6) << I
  71.             << "   Contents of node: "
  72.             << setw(6) << I->Int() << endl;
  73.     }
  74.  
  75.     main()
  76.     {
  77.         SBinder::registerClass();
  78.         StreamableInt::registerClass();
  79.  
  80.         // When debugging redirect cout to a file
  81.         // so that you can examine the trace!
  82.         cerr = cout;
  83.         // Let's watch on cerr for stream errors!
  84.         Streamable::streamDebug = 1;
  85.         // Let's watch the multiple linking -
  86.         // unlinking on cerr!
  87.         Streamable::refDebug = 1;
  88.         // Let's watch StreamableClassRegistry 
  89.         // errors on cerr!
  90.         StreamableClassRegistry::debug = 1;
  91.  
  92.         SBinder B(BDR_OK_FREE,ORIG_NODES);
  93.  
  94.         int i = 0;
  95.  
  96.         while (B.vacancyNonElastic())
  97.             B.push(new StreamableInt(i++));
  98.  
  99.         B.setMaxNodes();
  100.         
  101.         cout << "\n\nBinder with streamable "
  102.             << "nodes and \nmultiple links "
  103.             << "to those nodes, unsorted:\n\n";
  104.  
  105.         i = 0;
  106.  
  107.         while (i < ORIG_NODES)
  108.  
  109.             B.atIns(ORIG_NODES,B[i++]);
  110.  
  111.             // Like pushing a stack
  112.             // at ORIG_NODES.
  113.  
  114.  
  115.         cout << endl << endl;
  116.  
  117.         B.forEach((BDRforEachBlocK)display);
  118.  
  119.         B.setComparE((BDRcomparE)sintcmp);
  120.         
  121.         B.link();  // can't stream unless linked!
  122.  
  123.         RegisterFunction(ID_SINT_CMP,
  124.             (GenericFnC)sintcmp);
  125.  
  126.         ofstream oS("sbdrdem1.txt");
  127.         if (oS)  {
  128.  
  129.           oS << (StreamablE) B;
  130.  
  131.           B.restream();
  132.  
  133.           // Don't stream B again
  134.           // without restreaming!!!
  135.  
  136.           oS.close();
  137.           ifstream iS("sbdrdem1.txt");
  138.           if (iS)  {
  139.  
  140.             StreamablE C;
  141.             
  142.             cout << "\n\nStreamed and "
  143.             << "reloaded Binder with "
  144.             << "multiple links maintained "
  145.             << "\nand sorted with streamed "
  146.             << "compare fnc: \n\n";
  147.             
  148.             iS >> C;
  149.             iS.close();
  150.  
  151.             RestreamRegistry();
  152.  
  153.             // Don't load again from
  154.             // any stream with
  155.             // restreaming
  156.             // Registry!!!
  157.  
  158.             if (C)
  159.             {
  160.               ((SBindeR)C)->sort();
  161.               cout << endl << endl;
  162.               ((SBindeR)C)->forEach(
  163.             (BDRforEachBlocK)display);
  164.               delete (SBindeR) C;
  165.             }
  166.             else
  167.               cout << "\n\nUnable to reload"
  168.             << " Binder \n\n";
  169.           }
  170.           else
  171.             cout << "\n\nUnable to reopen"
  172.               << " stream for input of"
  173.               << " of Binder \n\n";
  174.         }
  175.         
  176.         B.unlink();  // not really necessary here
  177.  
  178.  
  179.         return 0;
  180.     }